<?php
require 'cabesario.php';
include 'model/Contact.php';
if (isset($_SESSION["autenticacao"])) {
    if ($_SESSION['autenticacao'] == "ok") {

        $activity = new Contact();
        $user = $_SESSION['usuario'];
        $busca = "";
        if (isset($_GET['busca'])) {
            $contacts = $activity->selectByName($user->getId(), $_GET['busca']);
            $busca = strlen($_GET['busca'])>0?"Resultado da busca por '" . $_GET['busca'] . "'":"";
        } else {
            $contacts = $activity->selectByUser($user->getId());
        }
        ?>
        <form method="get" action="mycontacts.php">
            <input class="input_text" style="width: 250px;" type="search" name="busca" placeholder="Pesquisar..." value="<?php echo isset($_GET['busca']) ? $_GET['busca'] : "" ?>">
            <input class="style_button" type="submit" value="Buscar">
        </form>
        <div class="busca"><?php echo $busca; ?></div>
        <?php
        if (count($contacts) > 0) {
            foreach ($contacts as $key => $a) {
                $description = $a->getName();
                $des = $a->getEmail() . " - " . $a->getFone()
                        ?><a href="contact.php?edit=<?php echo $a->getId() ?>" title="Editar">
                    <div id="contact">
                        <div class="name"><?php echo $description ?></div>
                        <div class="description"><?php echo $des ?></div>
                    </div>
                </a>
                <?php
            }
        } else {
            echo'<div id="message" class="info">Nenhum contato encontrado</div>';
        }
        ?>
    <?php
    }
} else {
    header("location: index.php");
}require 'rodape.php';
?>